package org.niu.listnode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class NodesBetween {
    public int[] nodesBetweenCriticalPoints(ListNode head) {

        if(head.next.next == null) return new int[]{-1,-1};

        ListNode p = head,p1 = head.next,p2=head.next.next;
        int cnt = 2;
        List<Integer> list = new ArrayList<>();
        while(p2!=null){
            if((p1.val > p2.val && p1.val > p.val)||(p1.val<p2.val&&p1.val<p.val)){
                list.add(cnt);
            }
            p = p.next;
            p1 = p1.next;
            p2 = p2.next;
            cnt++;
        }
        if(list.size()<=1) return new int[]{-1,-1};
        int ans1 = list.get(list.size()-1)-list.get(0);
        int ans2 = Integer.MAX_VALUE/2;
        for (int i = 0; i < list.size()-1; i++) {
            if(list.get(i+1)-list.get(i)<ans2){
                ans2 = list.get(i+1)-list.get(i);
            }
        }
        return new int[]{ans2,ans1};
    }
}
